import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import ActiveListComponet from '../components/ActiveListComponet.vue'
import IndexView from '../views/IndexView.vue'
import UserListComponet from '../components/UserListComponet.vue'
import ActiveList from '../components/ActiveList.vue'
import UserView from '../components/UserView.vue'
import UserAdd from '../components/UserAdd.vue'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [

    //首页
    {
      path: '/',
      meta: { isAutu: true },
      redirect: '/home',
    },
    {
      path: '/home',
      meta: { isAutu: true },
      component: IndexView,
      children: [
        {
          path: '',
          redirect: '/home/user',
        },
        {
          path: '/home/user',
          component: UserView,
          children: [
            {
              path: '',
              redirect: '/home/user/userList'
            },
            {
              path: '/home/user/userList',
              component: UserListComponet
            },
            {
              path: '/home/user/useradd',
              component: UserAdd
            }
          ]
        },
        {
          path: '/home/active',
          component: ActiveListComponet,
          children: [
            {
              path: '',
              redirect: '/home/active/activelist'
            },
            {
              path: '/home/active/activelist',
              component: ActiveList
            },
            {
              path: '/home/active/articleadd'
            }
          ]
        }

      ]
    },

    //登录
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    //注册
    {
      path: '/rejist',
      name: 'rejist',
      component: () => import('../views/RejistView.vue')
    }

  ]
})
router.beforeEach((to, from) => {
  if (to.meta.isAutu) {
    if (!window.localStorage.getItem('token')) {
      router.push('/login')
    }
  }
})

export default router
